package tr.edu.ege.seagent.sharedclipboard;

import javax.swing.JOptionPane;

import tr.edu.ege.communicator.Address;
import tr.edu.ege.communicator.Message;
import tr.edu.ege.communicator.Peer;

/**
 * 
 * Handles the received messages..
 * 
 * @author Onur ULUSU
 * 
 */
public class MessageProtocol {

	/**
	 * Peer of this application.
	 */
	private Peer peer;

	/**
	 * Processes the given message with the type of the message..
	 * 
	 * @param message
	 *            to be processed..
	 * @param sharedClipboard
	 *            application instance, to set labels..
	 * @param peer
	 *            of this application.
	 */
	public void processMessage(Message message,
			SharedClipboard sharedClipboard, Peer peer) {
		this.peer = peer;
		// Connection request status..
		if (message.getMessageType() == MessageTypes.CONNECTION_REQUEST) {
			// ask user whether (s)he accepts the connection..
			int answer = JOptionPane.showConfirmDialog(null,
					"A connection request is received from "
							+ message.getSender().getAddress(),
					"Connection Request", JOptionPane.YES_NO_OPTION);

			// connection accepted..
			if (answer == JOptionPane.YES_OPTION) {
				System.out.println("Connected to "
						+ message.getSender().toString());
				sharedClipboard.getStatusLabel().setText(
						"Connected to " + message.getSender().toString());
				sharedClipboard.setConnectedAdress(message.getSender());
				// send info to sender..
				sendResponseMessage(message, MessageTypes.CONNECTION_ACCEPTED);
				sharedClipboard.getConnectTo().setEnabled(false);
				sharedClipboard.getPasteMenuItem().setEnabled(true);

				// connection refused..
			} else {
				// send info to sender..
				sendResponseMessage(message, MessageTypes.CONNECTION_REFUSED);
			}
			// connection refused status..
		} else if (message.getMessageType() == MessageTypes.CONNECTION_REFUSED) {
			JOptionPane.showMessageDialog(null,
					"Connection request is refused..");
			// connection accepted status..
		} else if (message.getMessageType() == MessageTypes.CONNECTION_ACCEPTED) {
			System.out
					.println("Connected to " + message.getSender().toString());
			sharedClipboard.getStatusLabel().setText(
					"Connected to " + message.getSender().toString());
			sharedClipboard.setConnectedAdress(message.getSender());
			sharedClipboard.getConnectTo().setEnabled(false);
			sharedClipboard.getPasteMenuItem().setEnabled(true);
		} else if (message.getMessageType() == MessageTypes.MESSAGE) {
			sharedClipboard.getLabel().setText(message.getContent());
		}
	}

	/**
	 * Return a response message to the sender..
	 * 
	 * @param message
	 *            to be sent
	 * @param messageType
	 *            type of this response..
	 */
	private void sendResponseMessage(Message message, int messageType) {
		// creating a new message.
		Message response = new Message();
		// setting type of it..
		response.setMessageType(messageType);
		// getting IP address and port number of the sender..
		String hostAddress = message.getReciever().getAddress()
				.getHostAddress();
		int portNumber = message.getSender().getPortNumber();
		response.setReciever(new Address(hostAddress, portNumber));
		// sending message..
		peer.send(response);
	}
}